package com.miage.kangourou.client.calendar;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class MenuWidget extends GenericWidget {
	private HorizontalPanel menuPanel;
	private Label title;
	private Button previousWeek;
	private Button nextWeek;
	private Button today;
	private Button admin;
	private Hyperlink connection;
	private final Label username = new Label();
	private ClickListener lienHypertextHandler;

	public MenuWidget(KangourouPanel context) {
		super(context, "menu");
	}

	public void configureWidget() {
		menuPanel.setWidth("100%");

		lienHypertextHandler = new ClickListener() {

			public void onClick(Widget sender) {
				PopUpConnection popUpConnection = new PopUpConnection();
				popUpConnection.CreatePopUpConnection(context).show();
			}
		};

		connection.addClickListener(lienHypertextHandler);

		previousWeek.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				context.clickChangeDateButton(-7);
			}
		});

		nextWeek.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				context.clickChangeDateButton(7);
			}
		});

		today.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				context.clickChangeDateButton(-1);
			}
		});

	}

	public Widget createWidget() {
		menuPanel = new HorizontalPanel();
		menuPanel.setSpacing(2);

		title = new Label();
		title.setStyleName("bold");
		previousWeek = new Button(" << ");
		nextWeek = new Button(" >> ");
		today = new Button("Aujourd'hui");
		admin = new Button("Administrer l'application");
		connection = new Hyperlink("Connexion", false, "");
		username.setStyleName("bold");
		username.setWordWrap(false);
		menuPanel.add(previousWeek);
		menuPanel.add(today);
		menuPanel.add(nextWeek);
		menuPanel.add(title);
		menuPanel.add(admin);
		menuPanel.add(username);
		menuPanel.add(connection);
		menuPanel.setCellHorizontalAlignment(title,
				HasHorizontalAlignment.ALIGN_CENTER);
		menuPanel.setCellVerticalAlignment(title,
				HasVerticalAlignment.ALIGN_MIDDLE);
		menuPanel.setCellWidth(previousWeek, "1%");
		menuPanel.setCellWidth(nextWeek, "1%");
		menuPanel.setCellWidth(today, "1%");
		menuPanel.setCellWidth(connection, "1%");
		menuPanel.setCellWidth(admin, "1%");
		menuPanel.setCellWidth(username, "1%");
		menuPanel.setCellWidth(title, "100%");
		menuPanel.setWidth("100%");

		admin.setVisible(false);
		admin.setEnabled(false);

		return menuPanel;
	}

	public Hyperlink getConnection() {
		return connection;
	}

	public Button getNextWeek() {
		return nextWeek;
	}

	public Button getPreviousWeek() {
		return previousWeek;
	}

	public Label getTitle() {
		return title;
	}

	public Button getToday() {
		return today;
	}

	public void refreshAfterLogin() {

		username.setText(context.utilisateurEnCours.getPrenom() + " "
				+ context.utilisateurEnCours.getNom());
		connection.setText("Se déconnecter");

		connection.removeClickListener(lienHypertextHandler);

		lienHypertextHandler = new ClickListener() {

			@Override
			public void onClick(Widget sender) {
				context.utilisateurEnCours.kill();
				context.refreshAfterLogout();

			}
		};

		connection.addClickListener(lienHypertextHandler);

		if (context.utilisateurEnCours.isAdmin()) {
			admin.setVisible(true);
			admin.setEnabled(true);

			admin.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {

					PopUpAdmin popUpAdmin = new PopUpAdmin();
					popUpAdmin.createPopUpAdmin(context).show();
				}
			});
		}

	}

	public void refreshAfterLogout() {

		username.setText(context.utilisateurEnCours.getPrenom() + " "
				+ context.utilisateurEnCours.getNom());
		connection.setText("Connexion");

		connection.removeClickListener(lienHypertextHandler);

		lienHypertextHandler = new ClickListener() {

			@Override
			public void onClick(Widget sender) {
				PopUpConnection popUpConnection = new PopUpConnection();
				popUpConnection.CreatePopUpConnection(context).show();
			}
		};

		connection.addClickListener(lienHypertextHandler);

		admin.setVisible(false);
		admin.setEnabled(false);

	}
}
